package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.CountAdapter;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.Count;
import com.chaoyong.higo.bean.SharBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.view.TitleView;
import com.chaoyong.higo.view.XListView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class CountActivity extends BaseActivity {
	
	private ListView xlv;
	private CountAdapter adapter;
	private ArrayList<Count> data;
	private SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmss");
	private Long lon = 0l;
	private TextView total,quyu,jieguo,jiexiao;
	
	@Override
	public int layoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_count;
	}
	private Context context;
	private TitleView titleView;
	@Override
	public void findView() {
		// TODO Auto-generated method stub
		titleView=(TitleView) findViewById(R.id.count_titleview);
		xlv = (ListView) findViewById(R.id.count_xlv);
		total = (TextView) findViewById(R.id.count_total);
		quyu = (TextView) findViewById(R.id.count_quyu);
		jieguo = (TextView) findViewById(R.id.count_result);
		jiexiao = (TextView) findViewById(R.id.count_jiexiao);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		context=this;
		titleView.setTitleText("计算详情",context.getResources().getColor(R.color.home_text));
		titleView.setLeftImage(R.drawable.back_red);
		titleView.setonLeftClinck(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		adapter = new CountAdapter(this, null);
		xlv.setAdapter(adapter);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//appport/compute
		String goods_id = getIntent().getStringExtra("goods_id");
		String expect = getIntent().getStringExtra("expect");
		final int length = getIntent().getIntExtra("length", 1);
		String time = getIntent().getStringExtra("time");
		jiexiao.setText("截止揭晓时间【" + time + "】\n最后100条全站购买记录");
		JSONObject json = new JSONObject();
		try {
			json.put("model", "GoodsWinning");
			json.put("function", "winning_record_100");
			json.put("goods_id", goods_id);
			json.put("expect", expect);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_compute, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					JSONArray dataa = jso.getJSONArray("data");
					Gson gson = new GsonBuilder().create();
					data = new ArrayList<Count>();
					for (int i = 0; i < dataa.length(); i++) {
						//Count count = gson.fromJson(dataa.getJSONObject(i).toString(), Count.class);
						Count count = gson.fromJson(dataa.getJSONObject(i).toString(), new TypeToken<Count>() {
						}.getType());
						data.add(count);
					}
					adapter.changeData(data);
					for (int i = 0; i < 100; i++) {
						String date1 = data.get(i).getTime();
						String split1 = date1.split("\\.")[0];
						String split2 = date1.split("\\.")[1];
						lon += Long.parseLong(sdf2.format(new Date(Long.parseLong(split1) * 1000L)) + split2);
					}
					
					//total.setText("1、求和：" + lon + "（上面100条记录时间取值相加之和）");
					long he = jso.getLong("message");
					total.setText("1、求和：" + he + "（上面100条记录时间取值相加之和）");
					long yushu = he%length;
					quyu.setText("2、取余：" + he + "（100条时间记录之和）%" + length + "=" + yushu + "（余数）");
					jieguo.setText("3、结果：" + yushu + "（余数）" + "+10000001=" + (yushu + 10000001));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

}
